<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">


<link rel="stylesheet" href="/myblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/myblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/myblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gao-qianwei.gitee.io","root":"/myblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="一、HDFS Shell操作1.1基本语法   hadoop fs 具体命令    hdfs dfs 具体命令">
<meta property="og:type" content="article">
<meta property="og:title" content="HDFS生产环境中的操作">
<meta property="og:url" content="https://gao-qianwei.gitee.io/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="一、HDFS Shell操作1.1基本语法   hadoop fs 具体命令    hdfs dfs 具体命令">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/1.png">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/2.png">
<meta property="article:published_time" content="2021-05-22T08:19:27.158Z">
<meta property="article:modified_time" content="2021-05-22T08:29:59.832Z">
<meta property="article:author" content="高乾威">
<meta property="article:tag" content="数据研发">
<meta property="article:tag" content="Hadoop">
<meta property="article:tag" content="HDFS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gao-qianwei.gitee.io/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/1.png">

<link rel="canonical" href="https://gao-qianwei.gitee.io/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>HDFS生产环境中的操作 | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-links">

    <a href="/myblog/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gao-qianwei.gitee.io/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/images/avatar.png">
      <meta itemprop="name" content="高乾威">
      <meta itemprop="description" content="报我楼成秋望月，把君诗读夜回灯">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          HDFS生产环境中的操作
        </h1>

        <div class="post-meta">

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-05-22 16:19:27 / 修改时间：16:29:59" itemprop="dateCreated datePublished" datetime="2021-05-22T16:19:27+08:00">2021-05-22</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">数据研发</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/Hadoop/" itemprop="url" rel="index"><span itemprop="name">Hadoop</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/Hadoop/HDFS/" itemprop="url" rel="index"><span itemprop="name">HDFS</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <span class="post-meta-divider">|</span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="一、HDFS-Shell操作"><a href="#一、HDFS-Shell操作" class="headerlink" title="一、HDFS Shell操作"></a>一、HDFS Shell操作</h3><h4 id="1-1基本语法"><a href="#1-1基本语法" class="headerlink" title="1.1基本语法"></a>1.1基本语法</h4><ul>
<li>  hadoop fs 具体命令 </li>
<li>  hdfs dfs 具体命令 </li>
</ul>
<span id="more"></span>

<p>&ensp;&ensp;&ensp;&ensp;两个是完全相同的。</p>
<h4 id="1-2命令大全"><a href="#1-2命令大全" class="headerlink" title="1.2命令大全"></a>1.2命令大全</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">hdfs dfs</span><br><span class="line">          [-appendToFile &lt;localsrc&gt; ... &lt;dst&gt;]</span><br><span class="line">          [-cat [-ignoreCrc] &lt;src&gt; ...]</span><br><span class="line">          [-checksum &lt;src&gt; ...]</span><br><span class="line">          [-chgrp [-R] GROUP PATH...]</span><br><span class="line">          [-chmod [-R] &lt;MODE[,MODE]... | OCTALMODE&gt; PATH...]</span><br><span class="line">          [-chown [-R] [OWNER][:[GROUP]] PATH...]</span><br><span class="line">          [-copyFromLocal [-f] [-p] &lt;localsrc&gt; ... &lt;dst&gt;]</span><br><span class="line">          [-copyToLocal [-p] [-ignoreCrc] [-crc] &lt;src&gt; ... &lt;localdst&gt;]</span><br><span class="line">          [-count [-q] &lt;path&gt; ...]</span><br><span class="line">          [-cp [-f] [-p] &lt;src&gt; ... &lt;dst&gt;]</span><br><span class="line">          [-createSnapshot &lt;snapshotDir&gt; [&lt;snapshotName&gt;]]</span><br><span class="line">          [-deleteSnapshot &lt;snapshotDir&gt; &lt;snapshotName&gt;]</span><br><span class="line">          [-df [-h] [&lt;path&gt; ...]]</span><br><span class="line">          [-du [-s] [-h] &lt;path&gt; ...]</span><br><span class="line">          [-expunge]</span><br><span class="line">          [-get [-p] [-ignoreCrc] [-crc] &lt;src&gt; ... &lt;localdst&gt;]</span><br><span class="line">          [-getfacl [-R] &lt;path&gt;]</span><br><span class="line">          [-getmerge [-nl] &lt;src&gt; &lt;localdst&gt;]</span><br><span class="line">          [-help [cmd ...]]</span><br><span class="line">          [-ls [-d] [-h] [-R] [&lt;path&gt; ...]]</span><br><span class="line">          [-mkdir [-p] &lt;path&gt; ...]</span><br><span class="line">          [-moveFromLocal &lt;localsrc&gt; ... &lt;dst&gt;]</span><br><span class="line">          [-moveToLocal &lt;src&gt; &lt;localdst&gt;]</span><br><span class="line">          [-mv &lt;src&gt; ... &lt;dst&gt;]</span><br><span class="line">          [-put [-f] [-p] &lt;localsrc&gt; ... &lt;dst&gt;]</span><br><span class="line">          [-renameSnapshot &lt;snapshotDir&gt; &lt;oldName&gt; &lt;newName&gt;]</span><br><span class="line">          [-rm [-f] [-r|-R] [-skipTrash] &lt;src&gt; ...]</span><br><span class="line">          [-rmdir [--ignore-fail-on-non-empty] &lt;dir&gt; ...]</span><br><span class="line">          [-setfacl [-R] [&#123;-b|-k&#125; &#123;-m|-x &lt;acl_spec&gt;&#125; &lt;path&gt;]|[--set &lt;acl_spec&gt; &lt;path&gt;]]</span><br><span class="line">          [-setrep [-R] [-w] &lt;rep&gt; &lt;path&gt; ...]</span><br><span class="line">          [-stat [format] &lt;path&gt; ...]</span><br><span class="line">          [-tail [-f] &lt;file&gt;]</span><br><span class="line">          [-test -[defsz] &lt;path&gt;]</span><br><span class="line">          [-text [-ignoreCrc] &lt;src&gt; ...]</span><br><span class="line">          [-touchz &lt;path&gt; ...]</span><br><span class="line">          [-usage [cmd ...]]</span><br><span class="line">12345678910111213141516171819202122232425262728293031323334353637</span><br></pre></td></tr></table></figure>

<h4 id="1-3常用命令"><a href="#1-3常用命令" class="headerlink" title="1.3常用命令"></a>1.3常用命令</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">-help：查看命令的使用方式</span><br><span class="line">hdfs dfs -help rm</span><br><span class="line"></span><br><span class="line">-ls: 显示目录信息</span><br><span class="line">hdfs dfs -ls /</span><br><span class="line"></span><br><span class="line">-mkdir：在hdfs上创建目录</span><br><span class="line">hdfs dfs -mkdir /bigdata</span><br><span class="line">hdfs dfs -mkdir -p /kgc/test</span><br><span class="line"></span><br><span class="line">-moveFromLocal从本地剪切粘贴到HDFS文件系统</span><br><span class="line">hdfs dfs -moveFromLocal  ./hello.txt  /kgc/test</span><br><span class="line"></span><br><span class="line">-appendToFile  ：追加一个文件到已经存在的文件末尾</span><br><span class="line">hdfs dfs -appendToFile test1.txt /kgc/test/hello.txt</span><br><span class="line"></span><br><span class="line">-cat ：显示文件内容</span><br><span class="line">hdfs dfs -cat /kgc/test/hello.txt</span><br><span class="line"></span><br><span class="line">-tail：显示一个文件的末尾</span><br><span class="line">hdfs dfs -tail /kgc/test/hello.txt</span><br><span class="line"></span><br><span class="line">-chgrp 、-chmod、-chown：linux文件系统中的用法一样，修改文件所属权限</span><br><span class="line">hdfs dfs -chmod  666  /kgc/test/hello.txt</span><br><span class="line">hdfs dfs -chown  kgc:kgc   /kgc/test/hello.txt</span><br><span class="line"></span><br><span class="line">-copyFromLocal：从本地文件系统中拷贝文件到hdfs路径去</span><br><span class="line">hdfs dfs -copyFromLocal README.txt /</span><br><span class="line"></span><br><span class="line">-copyToLocal：从hdfs拷贝到本地</span><br><span class="line">hdfs dfs -copyToLocal /kgc/test/hello.txt ./</span><br><span class="line"></span><br><span class="line">-cp ：从hdfs的一个路径拷贝到hdfs的另一个路径</span><br><span class="line">hdfs dfs -cp /kgc/test/hello.txt /hello1.txt</span><br><span class="line"></span><br><span class="line">-mv：在hdfs目录中移动文件</span><br><span class="line">hdfs dfs -mv /hello1.txt /kgc/test/</span><br><span class="line"></span><br><span class="line">-get：等同于copyToLocal，就是从hdfs下载文件到本地</span><br><span class="line">hdfs dfs -get /kgc/test/hello.txt ./</span><br><span class="line"></span><br><span class="line">-getmerge  ：合并下载多个文件，比如hdfs的目录 /aaa/下有多个文件:log.1, log.2,log.3,...</span><br><span class="line">hdfs dfs -getmerge /user/kgc/test/* ./merge.txt</span><br><span class="line"></span><br><span class="line">-put：等同于copyFromLocal</span><br><span class="line">hdfs dfs -put ./merge.txt /user/kgc/test/</span><br><span class="line"></span><br><span class="line">-rm：删除文件或文件夹</span><br><span class="line">hdfs dfs -rm /user/kgc/test/merge.txt</span><br><span class="line"></span><br><span class="line">-rmdir：删除空目录</span><br><span class="line">hdfs dfs -rmdir /test</span><br><span class="line"></span><br><span class="line">-du统计文件夹的大小信息</span><br><span class="line">hdfs dfs -du -s -h /user/kgc/test</span><br><span class="line">hdfs dfs -du -h /user/kgc/test</span><br><span class="line"></span><br><span class="line">-setrep：设置hdfs中文件的副本数量</span><br><span class="line">hdfs dfs -setrep 10 /kgc/test/hello.txt</span><br><span class="line">1234567891011121314151617181920212223242526272829303132333435363738394041</span><br></pre></td></tr></table></figure>



<h3 id="二、HDFS-的-API-操作"><a href="#二、HDFS-的-API-操作" class="headerlink" title="二、HDFS 的 API 操作"></a>二、HDFS 的 API 操作</h3><h4 id="2-1-客户端环境准备"><a href="#2-1-客户端环境准备" class="headerlink" title="2.1 客户端环境准备"></a>2.1 客户端环境准备</h4><p>&ensp;&ensp;&ensp;&ensp;1）找到资料包路径下的 Windows 依赖文件夹，拷贝 hadoop-3.1.0 到非中文路径（比如 d:\）。 </p>
<p>&ensp;&ensp;&ensp;&ensp;2）配置 HADOOP_HOME 环境变量</p>
<p><img src="/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/1.png" alt="image-20210520193441823"></p>
<p>&ensp;&ensp;&ensp;&ensp;3）配置 Path 环境变量。</p>
<p><img src="/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E6%93%8D%E4%BD%9C/2.png" alt="image-20210520193459128"></p>
<p>&ensp;&ensp;&ensp;&ensp;验证 Hadoop 环境变量是否正常。双击 winutils.exe，如果页面一闪而过不报错，就是正常的。</p>
<p>&ensp;&ensp;&ensp;&ensp;4）在 IDEA 中创建一个 Maven 工程 HdfsClientDemo，并导入相应的依赖坐标+日志添加</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.hadoop<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>hadoop-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.1.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.12<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.30<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;在项目的 src/main/resources 目录下，新建一个文件，命名为“log4j.properties”，在文件 中填入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">log4j.rootLogger&#x3D;INFO, stdout </span><br><span class="line">log4j.appender.stdout&#x3D;org.apache.log4j.ConsoleAppender </span><br><span class="line">log4j.appender.stdout.layout&#x3D;org.apache.log4j.PatternLayout </span><br><span class="line">log4j.appender.stdout.layout.ConversionPattern&#x3D;%d %p [%c] - %m%n </span><br><span class="line">log4j.appender.logfile&#x3D;org.apache.log4j.FileAppender </span><br><span class="line">log4j.appender.logfile.File&#x3D;target&#x2F;spring.log </span><br><span class="line">log4j.appender.logfile.layout&#x3D;org.apache.log4j.PatternLayout </span><br><span class="line">log4j.appender.logfile.layout.ConversionPattern&#x3D;%d %p [%c] - %m%n</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;5）创建包名：com.james.hdfs </p>
<p>&ensp;&ensp;&ensp;&ensp;6）创建 HdfsClient 类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HdfsClient</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testMkdirs</span><span class="params">()</span> <span class="keyword">throws</span> IOException, URISyntaxException, </span></span><br><span class="line"><span class="function">InterruptedException </span>&#123;</span><br><span class="line">    <span class="comment">// 1 获取文件系统</span></span><br><span class="line">    Configuration configuration = <span class="keyword">new</span> Configuration();</span><br><span class="line">    <span class="comment">// FileSystem fs = FileSystem.get(new URI(&quot;hdfs://hadoop102:8020&quot;), configuration);</span></span><br><span class="line">    FileSystem fs = FileSystem.get(<span class="keyword">new</span> URI(<span class="string">&quot;hdfs://hadoop102:8020&quot;</span>), configuration,<span class="string">&quot;atguigu&quot;</span>);</span><br><span class="line">    <span class="comment">// 2 创建目录</span></span><br><span class="line">    fs.mkdirs(<span class="keyword">new</span> Path(<span class="string">&quot;/xiyou/huaguoshan/&quot;</span>));</span><br><span class="line">    <span class="comment">// 3 关闭资源</span></span><br><span class="line">    fs.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;7）执行程序：客户端去操作 HDFS 时，是有一个用户身份的。默认情况下，HDFS 客户端 API 会从采 用 Windows 默认用户访问 HDFS，会报权限异常错误。所以在访问 HDFS 时，一定要配置用户。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">org.apache.hadoop.security.AccessControlException: Permission denied: </span><br><span class="line">user&#x3D;56576, access&#x3D;WRITE, </span><br><span class="line">inode&#x3D;&quot;&#x2F;xiyou&#x2F;huaguoshan&quot;:james:supergroup:drwxr-xr-x</span><br></pre></td></tr></table></figure>



<h4 id="2-2HDFS的API操作"><a href="#2-2HDFS的API操作" class="headerlink" title="2.2HDFS的API操作"></a>2.2HDFS的API操作</h4><p>&ensp;&ensp;&ensp;&ensp;HDFS 文件上传（测试参数优先级）</p>
<p>&ensp;&ensp;&ensp;&ensp;1）编写源代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testCopyFromLocalFile</span><span class="params">()</span> <span class="keyword">throws</span> IOException, </span></span><br><span class="line"><span class="function">InterruptedException, URISyntaxException </span>&#123;</span><br><span class="line">    <span class="comment">// 1 获取文件系统</span></span><br><span class="line">    Configuration configuration = <span class="keyword">new</span> Configuration();</span><br><span class="line">    configuration.set(<span class="string">&quot;dfs.replication&quot;</span>, <span class="string">&quot;2&quot;</span>);</span><br><span class="line">    FileSystem fs = FileSystem.get(<span class="keyword">new</span> URI(<span class="string">&quot;hdfs://hadoop102:8020&quot;</span>), configuration, <span class="string">&quot;atguigu&quot;</span>);</span><br><span class="line">    <span class="comment">// 2 上传文件</span></span><br><span class="line">    fs.copyFromLocalFile(<span class="keyword">new</span> Path(<span class="string">&quot;d:/sunwukong.txt&quot;</span>), <span class="keyword">new</span> Path(<span class="string">&quot;/xiyou/huaguoshan&quot;</span>));</span><br><span class="line">   <span class="comment">// 3 关闭资源</span></span><br><span class="line">    fs.close();</span><br><span class="line">｝</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;2）将 hdfs-site.xml 拷贝到项目的 resources 资源目录下</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;configuration.xsl&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">name</span>&gt;</span>dfs.replication<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">value</span>&gt;</span>1<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>&ensp;&ensp;&ensp;&ensp;3）参数优先级 </p>
<p>&ensp;&ensp;&ensp;&ensp;参数优先级排序：（1）客户端代码中设置的值 &gt;（2）ClassPath 下的用户自定义配置文 件 &gt;（3）然后是服务器的自定义配置（xxx-site.xml）&gt;（4）服务器的默认配置（xxx-default.xml）</p>
<p>&ensp;&ensp;&ensp;&ensp;HDFS 文件下载</p>
<p>&ensp;&ensp;&ensp;&ensp;HDFS 文件更名和移动</p>
<p>&ensp;&ensp;&ensp;&ensp;HDFS 删除文件和目录</p>
<p>&ensp;&ensp;&ensp;&ensp;HDFS 文件详情查看</p>
<p>&ensp;&ensp;&ensp;&ensp;HDFS 文件和文件夹判断</p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Qp4y1n7EN?p=1">尚硅谷丨大数据Hadoop 3.x（2021全新升级/部署+源码+实战）_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili</a></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/myblog/tags/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/" rel="tag"># 数据研发</a>
              <a href="/myblog/tags/Hadoop/" rel="tag"># Hadoop</a>
              <a href="/myblog/tags/HDFS/" rel="tag"># HDFS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/myblog/2021/05/22/Hadoop%E4%B9%8BHDFS%EF%BC%9AHDFS%E6%A6%82%E8%BF%B0/" rel="prev" title="HDFS简介">
      <i class="fa fa-chevron-left"></i> HDFS简介
    </a></div>
      <div class="post-nav-item">
    <a href="/myblog/2021/05/22/Hadoop%EF%BC%9AHadoop%E7%AE%80%E4%BB%8B/" rel="next" title="Hadoop简介">
      Hadoop简介 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E3%80%81HDFS-Shell%E6%93%8D%E4%BD%9C"><span class="nav-text">一、HDFS Shell操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="nav-text">1.1基本语法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2%E5%91%BD%E4%BB%A4%E5%A4%A7%E5%85%A8"><span class="nav-text">1.2命令大全</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-text">1.3常用命令</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E3%80%81HDFS-%E7%9A%84-API-%E6%93%8D%E4%BD%9C"><span class="nav-text">二、HDFS 的 API 操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="nav-text">2.1 客户端环境准备</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2HDFS%E7%9A%84API%E6%93%8D%E4%BD%9C"><span class="nav-text">2.2HDFS的API操作</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="高乾威"
      src="/myblog/images/avatar.png">
  <p class="site-author-name" itemprop="name">高乾威</p>
  <div class="site-description" itemprop="description">报我楼成秋望月，把君诗读夜回灯</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">59</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="http://github.com/GaoQianwei" title="GitHub → http:&#x2F;&#x2F;github.com&#x2F;GaoQianwei" rel="noopener" target="_blank">GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2966807184@qq.com" title="E-Mail → mailto:2966807184@qq.com" rel="noopener" target="_blank">E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/xue-wu-zhi-jing-75-22" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;xue-wu-zhi-jing-75-22" rel="noopener" target="_blank">知乎</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45341339" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45341339" rel="noopener" target="_blank">CSDN</a>
      </span>
  </div>



      </div><div>
  <canvas id="canvasDiyBlock" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
<script src="/myblog/js/custom/clock.js"></script>

</div>


    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021-04 到 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">高乾威</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

<span class="post-meta-divider">|</span>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 117.2k 字</span>
</div>

<span class="post-meta-divider">|</span>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/myblog/lib/anime.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/myblog/js/utils.js"></script>

<script src="/myblog/js/motion.js"></script>


<script src="/myblog/js/schemes/pisces.js"></script>


<script src="/myblog/js/next-boot.js"></script>


  <script defer src="/myblog/lib/three/three.min.js"></script>
    <script defer src="/myblog/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/myblog/js/local-search.js"></script>













  

  

</body>
</html>
